package com.guyu.plugin.password.crypto.impl;


import com.guyu.plugin.password.crypto.AbstractCrypto;
import org.dromara.hutool.crypto.asymmetric.KeyType;
import org.dromara.hutool.crypto.asymmetric.SM2;
import org.dromara.hutool.crypto.bc.SmUtil;
import org.dromara.hutool.crypto.digest.SM3;

import java.nio.charset.StandardCharsets;

public class SmCrypto extends AbstractCrypto {
    private static final long serialVersionUID = -810535984131734099L;
    private SM2 sm2;
    private SM3 sm3;

    public SmCrypto(String publicKeyStr, String privateKeyStr, String salt) {
        super(publicKeyStr, privateKeyStr, salt);
        this.sm2 = SmUtil.sm2(privateKeyStr, publicKeyStr);
        this.sm3 = SmUtil.sm3WithSalt(salt.getBytes(StandardCharsets.UTF_8));
    }

    @Override
    public String encryptHex(String data) {
        return sm2.encryptHex(data, KeyType.PublicKey);
    }

    @Override
    public String decryptStr(String data) {
        return sm2.decryptStr(data, KeyType.PrivateKey);
    }

    /**
     * SM3摘要
     *
     * @param data 被摘要数据
     * @return 256位摘要
     */
    @Override
    public String digestHex(String data) {
        return sm3.digestHex(data);
    }
}
